﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Models;
using Microhard.FMCG.Buz.Interfaces;
using HtmlAgilityPack;

namespace Microhard.FMCG.Buz.Impls.KASystem.WUMart
{
    public class SaleVolumnOfDayParser : IHtmlParser<List<StatementModel>>
    {
        public List<StatementModel> ParseObject(string html)
        {
            List<StatementModel> result = new List<StatementModel>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            //<td>供商编码</td><td>供商名称</td><td>门店编码</td><td>商品编码</td><td>商品名称</td><td>月至今销售金额
            //</td><td>当前库存</td><td>库存金额</td><td>门店名称</td><td>K库存数量</td><td>销售数量</td><td>有效日期</td><td>月份</td>
            var datas = doc.DocumentNode.Descendants("tr").ToList();
            for (int n = 1; n < datas.Count; n++)
            {
                var tds = datas[n].Descendants("td").ToList();
                if (tds.Count == 13)
                {
                    int code = 0;
                    if (int.TryParse(tds[0].InnerText, out code))
                    {
                        StatementModel sm = new StatementModel
                        {
                            SupplierCode = tds[0].InnerText,
                            SupplierName = tds[1].InnerText,
                            StoreCode = tds[2].InnerText,
                            ProductCode = tds[3].InnerText,
                            ProductName = tds[4].InnerText,
                            SaleVolumn = decimal.Parse(tds[5].InnerText),
                            CurrentStoreValue = float.Parse(tds[6].InnerText),
                            StoreValueVolumn = decimal.Parse(tds[7].InnerText),
                            StoreName = tds[8].InnerText,
                            KStoreValue = float.Parse(tds[9].InnerText),
                            SalueVolumnCount = float.Parse(tds[10].InnerText),
                            HQCompanyCode = System.Configuration.ConfigurationManager.AppSettings["WUMartCode"],
                            CurrentMonth = new DateTime(int.Parse(tds[12].InnerText.Substring(0, 4)), int.Parse(tds[12].InnerText.Substring(4, 2)), 1)
                        };
                        result.Add(sm);
                    }
                }
            }
            return result;
        }
    }
}
